package org.skyeye.modeling.parts;

import java.beans.PropertyChangeEvent;
import java.util.Collections;
import java.util.List;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.skyeye.modeling.dialogs.PropertyConfigDialog;
import org.skyeye.modeling.figures.BorderAnchor;
import org.skyeye.modeling.figures.DeviceFigure;
import org.skyeye.modeling.model.MNode;
import org.skyeye.modeling.model.MRoot;
import org.skyeye.modeling.model.MBase;
import org.skyeye.modeling.model.MDevice;
import org.skyeye.modeling.model.MLinker;
import org.skyeye.modeling.model.MSOC;
import org.skyeye.modeling.policies.EditPartUtil;
import org.skyeye.modeling.policies.NodeDirectEditPolicy;
import org.skyeye.modeling.policies.NodeEditPolicy;
import org.skyeye.modeling.policies.NodeGraphicalNodeEditPolicy;
import org.skyeye.modeling.policies.RootLayoutEditPolicy;

public class MLinkerPart extends AbstractPart {

	public void performRequest(Request req) {
		if(req.getType().equals(RequestConstants.REQ_OPEN)){
	    	if(!PropertyConfigDialog.isOpened){
	    		PropertyConfigDialog dialog = new PropertyConfigDialog(getViewer().getControl().getShell());
	    		MLinker model = (MLinker)getModel();
		    	
	    		MBase parent = (MBase) model.getParent();
	    		MBase root = null;
		    	if(parent instanceof MRoot){
		    		root = parent;
		    	}else if ( parent instanceof MSOC){
		    		parent = parent.getParent();
		    		if(parent instanceof MRoot){
		    			root = parent;
		    		}
		    		
		    	}
		    	if(root != null){
		    		dialog.setSource((MRoot)root);
		    		dialog.open();	
		    	}
		    	
	    	}
	    }else if(req.getType().equals(RequestConstants.REQ_DELETE)){
	    	System.out.println("delete in MLinkerPart");
	    }
	}

	public void propertyChange(PropertyChangeEvent evt) {
		super.propertyChange(evt);
		String pName = evt.getPropertyName();
		System.out.println("MLinkerPart: propertyChange "+pName);
		if (pName.equals(MNode.PROP_LOCATION)){
			/*this.getParent().refresh();	*/
			this.getParent().refresh();	
			
		}
		if(pName.equals(MDevice.PROP_SIZE)){
			this.refreshAll();
		}
		if(pName.equals(MDevice.PROP_NAME) || pName.equals(MDevice.PROP_BASE_TYPE)|| pName.equals(MDevice.PROP_CLASS_NAME)  || pName.equals(MDevice.PROP_CHANGE) ){
			this.refreshChildren();  // important!!!
			this.refreshAll();
		}
	}

	protected IFigure createFigure() {
		return new DeviceFigure((MDevice)this.getModel());
	}
	
	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new NodeGraphicalNodeEditPolicy());
		installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new NodeDirectEditPolicy());
        installEditPolicy(EditPolicy.COMPONENT_ROLE, new NodeEditPolicy());
	}

	public void activate() {
		super.activate();
		if (isActive()) {
			return;
		}
		((MNode) getModel()).addPropertyChangeListener(this);
	}

	public void deactivate() {
		super.deactivate();
		if (!isActive()) {
			return;
		}
		((MNode) getModel()).removePropertyChangeListener(this);
	}

	protected void refreshVisuals() {
		System.out.println("MLinkerPart refreshVisuals");
		refreshParentModel();
		DeviceFigure nf = (DeviceFigure) this.getFigure();
		MDevice node = (MDevice) getModel();
		Point loc = node.getLocation();
		Dimension size = node.getSize();
		Rectangle rectangle = new Rectangle(loc, size);
		
		nf.setName(((MNode) this.getModel()).getName());
		((GraphicalEditPart) getParent()).setLayoutConstraint(this, nf, rectangle);
	}

	public void refreshParentModel(){
		this.getParent().refresh();
	}

	public void refreshAll(){
		this.refresh();
	}
	
	public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connection) {
		return new BorderAnchor(getFigure());
	}

	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		return new BorderAnchor(getFigure());
	}

	public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart connection) {
		return new BorderAnchor(getFigure());
	}

	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		return new BorderAnchor(getFigure());
	}

	protected MLinker getSubject() {
		return (MLinker) getModel();
	}

	protected List getModelSourceConnections() {
		return getSubject().getShowOutgoingConnections();
	}

	protected List getModelTargetConnections() {
		return getSubject().getShowIncomingConnections();
	}
}